package top.codedance.iotp.tools.terminal.ui;

import top.codedance.iotp.tools.terminal.TerminalApplication;

import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author xuedianfu
 */
public class SetSkin extends javax.swing.JDialog {

    private ShellFrame sf;
    private Map<String, Map<String, String>> settings;

    /**
     * Creates new form SetSkin
     */
    public SetSkin(ShellFrame sf) {
        super(sf, true);
        this.sf = sf;
        initComponents();
        URL url = Thread.currentThread().getContextClassLoader().getResource("logo.gif");
        ImageIcon icon = new ImageIcon(url);
        setIconImage(icon.getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setMinimumSize(new Dimension(400, 50));
        setLocationRelativeTo(this.sf);
        setTitle("代码飞舞采控系统-设置皮肤");

        init();
    }

    private void init(){
        settings = TerminalApplication.readConfig();
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            jcb_skin.addItem(info.getName());
        }
        if(settings.containsKey("ui")){
            Map<String, String> ui = settings.get("ui");
            if(ui.containsKey("skin")){
                jcb_skin.setSelectedItem(ui.get("skin"));
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jcb_skin = new javax.swing.JComboBox<>();
        jLabel1 = new javax.swing.JLabel();
        jb_save = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("请选取皮肤：");

        jb_save.setText("保存");
        jb_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_saveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcb_skin, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_save)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcb_skin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jb_save))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jb_saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_saveActionPerformed
        if(settings.containsKey("ui")){
            Map<String, String> ui = settings.get("ui");
            ui.put("skin", (String)jcb_skin.getSelectedItem());
        }else{
            Map<String, String> ui = new HashMap<>();
            ui.put("skin", (String)jcb_skin.getSelectedItem());
            settings.put("ui", ui);
        }
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if (jcb_skin.getSelectedItem().equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    SwingUtilities.updateComponentTreeUI(this);
                    SwingUtilities.updateComponentTreeUI(sf);
                    break;
                }
            }
            TerminalApplication.saveConfig(settings);
            JOptionPane.showMessageDialog(this.getRootPane(), "保存成功");
            setVisible(false);
        }catch (Exception e){
            TerminalApplication.syslogInfo(e.getMessage());
            JOptionPane.showMessageDialog(this.getRootPane(), "保存失败");
        }
    }//GEN-LAST:event_jb_saveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton jb_save;
    private javax.swing.JComboBox<String> jcb_skin;
    // End of variables declaration//GEN-END:variables
}
